<template>
  <div >
    <a-spin :spinning="confirmLoading">
    <!--查询区域-->
    <div class="jeecg-basic-table-form-container">
      <a-form @finish="searchQuery" :model="queryParam" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-date-picker v-model:value="queryParam.date" picker="month" />
        <!-- <a-range-picker v-model:value="queryParam.timeRange" show-time/> -->
        <a-button class="search-btn" type="primary" html-type="submit">查询</a-button>
      </a-form>
      <a-button type="primary" @click="handleSynchronize">同步数据</a-button>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleSetting" preIcon="ant-design:plus-outlined"> 渠道设置</a-button>
      </template>
      <!--操作栏-->z
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <ChannelSettlementInfoModal ref="registerModal" @success="handleSuccess" />
    </a-spin>
  </div>

</template>

<script lang="ts" name="settlement-channelInfo" setup>
  import { ref, reactive } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useRouter } from 'vue-router';
  import { dateFormat } from '/@/utils/common/compUtils';
  import { columns } from './ChannelSettlementInfo.data';
  import { useMethods } from '/@/hooks/system/useMethods';
  import { list, synchronizeLastMonth, confirm } from './ChannelSettlementInfo.api';
  import ChannelSettlementInfoModal from './components/ChannelSettlementInfoModal.vue';
  import dayjs from 'dayjs';
  const registerModal = ref();
  const queryParam = ref<any>({
    date: dayjs().add(-1,'month'),
  });
  const router = useRouter();
  const confirmLoading = ref<boolean>(false);
  const { handleExportXls } = useMethods();
  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: 'channel_settlement_info',
      api: list,
      columns,
      canResize: false,
      useSearchForm: false,
      actionColumn: {
        width: 250,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        const date = dateFormat(queryParam.value.date, 'yyyy-MM');
        return Object.assign(params, {
          date,
          pageIndex: params.pageNo,
        });
      },
    },
  });
  const [registerTable, { reload }] = tableContext;
  const labelCol = reactive({
    xs: { span: 24 },
    sm: { span: 7 },
  });
  const wrapperCol = reactive({
    xs: { span: 24 },
    sm: { span: 16 },
  });

  /**
   * 新增事件
   */
  function handleSetting() {
    router.push({
      name: 'settlement-channelConfig',
    });
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    if (record.state === 'CHECKED') {
      // 保费结算单  经纪费明细  经纪费结算单
      return [
        // {
        //   label: '保费结算单',
        //   onClick: () => {
        //     console.log('保费结算单', record);
        //   },
        // },
        {
          label: '经纪费明细',
          onClick: () => {
            handleExportXls('经纪费明细', `/settlement/channel/exportXlsBrokerageFeeInfo?id=${record.id}`);
            console.log('经纪费明细', record);
          },
        },
        {
          label: '经纪费结算单',
          onClick: () => {
            handleExportXls('经纪费结算单', `/settlement/channel/exportXlsBrokerageFeeSettlementInfo?id=${record.id}`);
            console.log('经纪费结算单', record);
          },
        },
      ];
    }
    if (record.state === 'CHECKING') {
      return [
        {
          label: '保单明细',
          onClick: () => {
            handleExportXls('保单明细', `/settlement/channel/exportXlsPolicyInfo?id=${record.id}`);
            // window.open();
            console.log('保单明细', record);
          },
        },
        {
          label: '修改保单',
          onClick: handleEdit.bind(null, record),
        }
      ];
    }
    // <JUpload max-count="1" v-model:value="formData.wenjian"></JUpload>
    return [
      {
        label: '上传保单',
        onClick: handleAdd.bind(null, record),
      },
    ];
  }

  function handleConfirm(record: Recordable) {
    confirm({
      id: record.id,
    }).then(() => {
      reload();
    });
  }

  /**
   * 新增事件
   */
  function handleAdd(record: Recordable) {
    registerModal.value.add(record);
  }

  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    registerModal.value.edit(record);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    reload();
  }

  /**
   * 查询
   */
  function searchQuery() {
    reload();
  }
  async function handleSynchronize() {
    confirmLoading.value = true;
    synchronizeLastMonth().then(() => {
      reload();
    }).finally(() => {
      confirmLoading.value = false;
    });;
  }
</script>

<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #fff;
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }
    .query-group-cust {
      width: calc(50% - 15px);
      min-width: 100px !important;
    }
    .query-group-split-cust {
      width: 30px;
      display: inline-block;
      text-align: center;
    }
  }
</style>
